package com.dt.platform.oa.controller;

import java.util.*;
import com.dt.platform.proxy.oa.ProjectTaskServiceProxy;
import com.github.foxnic.commons.lang.StringUtil;
import com.github.foxnic.dao.data.Rcd;
import com.github.foxnic.dao.data.RcdSet;
import org.github.foxnic.web.domain.hrm.Person;
import org.github.foxnic.web.framework.web.SuperController;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import com.github.foxnic.commons.collection.CollectorUtil;
import com.github.foxnic.dao.entity.ReferCause;
import com.github.foxnic.api.swagger.InDoc;
import org.github.foxnic.web.framework.sentinel.SentinelExceptionUtil;
import com.github.foxnic.api.swagger.ApiParamSupport;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.dt.platform.proxy.oa.ProjectUserServiceProxy;
import com.dt.platform.domain.oa.meta.ProjectUserVOMeta;
import com.dt.platform.domain.oa.ProjectUser;
import com.dt.platform.domain.oa.ProjectUserVO;
import com.github.foxnic.api.transter.Result;
import com.github.foxnic.dao.data.SaveMode;
import com.github.foxnic.dao.excel.ExcelWriter;
import com.github.foxnic.springboot.web.DownloadUtil;
import com.github.foxnic.dao.data.PagedList;
import java.util.Date;
import java.sql.Timestamp;
import com.github.foxnic.api.error.ErrorDesc;
import com.github.foxnic.commons.io.StreamUtil;
import java.util.Map;
import com.github.foxnic.dao.excel.ValidateResult;
import java.io.InputStream;
import com.dt.platform.domain.oa.meta.ProjectUserMeta;
import org.github.foxnic.web.domain.hrm.Employee;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiImplicitParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.dt.platform.oa.service.IProjectUserService;
import com.github.foxnic.api.validate.annotations.NotNull;

/**
 * <p>
 * 项目人员接口控制器
 * </p>
 * @author 金杰 , maillank@qq.com
 * @since 2025-06-19 08:00:24
 */
@InDoc
@Api(tags = "项目人员")
@RestController("OaProjectUserController")
public class ProjectUserController extends SuperController {

    @Autowired
    private IProjectUserService projectUserService;

    /**
     * 添加项目人员
     */
    @ApiOperation(value = "添加项目人员")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectUserVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.IN_TIME, value = "加入时间", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.OUT_TIME, value = "离开时间", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.PROJECT_ID, value = "项目名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.STATUS, value = "人员状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.USER_ID, value = "项目人员", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.OWN_TYPE, value = "归属类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.ROLE, value = "角色", required = false, dataTypeClass = String.class)
	})
    @ApiParamSupport(ignoreDBTreatyProperties = true, ignoreDefaultVoProperties = true, ignorePrimaryKey = true)
    @ApiOperationSupport(order = 1, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = ProjectUserServiceProxy.INSERT, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectUserServiceProxy.INSERT)
    public Result insert(ProjectUserVO projectUserVO) {
        Result result = projectUserService.insert(projectUserVO, false);
        return result;
    }

    /**
     * 删除项目人员
     */
    @ApiOperation(value = "删除项目人员")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectUserVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class)
	})
    @ApiOperationSupport(order = 2, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = ProjectUserServiceProxy.DELETE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectUserServiceProxy.DELETE)
    public Result deleteById(String id) {
        this.validator().asserts(id).require("缺少id值");
        if (this.validator().failure()) {
            return this.validator().getFirstResult();
        }
        // 引用校验
        ReferCause cause = projectUserService.hasRefers(id);
        // 判断是否可以删除
        this.validator().asserts(cause.hasRefer()).requireEqual("不允许删除当前记录：" + cause.message(), false);
        if (this.validator().failure()) {
            return this.validator().getFirstResult().messageLevel4Confirm();
        }
        Result result = projectUserService.deleteByIdLogical(id);
        return result;
    }

    @ApiOperation(value = "动作")
    @ApiOperationSupport(order = 2, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = ProjectUserServiceProxy.ACTION, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectUserServiceProxy.ACTION)
    public Result action(String id, String action) {
        Result result = projectUserService.action(id, action);
        return result;
    }

    /**
     * 批量删除项目人员 <br>
     * 联合主键时，请自行调整实现
     */
    @ApiOperation(value = "批量删除项目人员")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectUserVOMeta.IDS, value = "主键清单", required = true, dataTypeClass = List.class, example = "[1,3,4]")
	})
    @ApiOperationSupport(order = 3, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = ProjectUserServiceProxy.DELETE_BY_IDS, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectUserServiceProxy.DELETE_BY_IDS)
    public Result deleteByIds(List<String> ids) {
        // 参数校验
        this.validator().asserts(ids).require("缺少ids参数");
        if (this.validator().failure()) {
            return this.validator().getFirstResult();
        }
        // 查询引用
        Map<String, ReferCause> causeMap = projectUserService.hasRefers(ids);
        // 收集可以删除的ID值
        List<String> canDeleteIds = new ArrayList<>();
        for (Map.Entry<String, ReferCause> e : causeMap.entrySet()) {
            if (!e.getValue().hasRefer()) {
                canDeleteIds.add(e.getKey());
            }
        }
        // 执行删除
        if (canDeleteIds.isEmpty()) {
            // 如果没有一行可以被删除
            return ErrorDesc.failure().message("无法删除您选中的数据行：").data(0).addErrors(CollectorUtil.collectArray(CollectorUtil.filter(causeMap.values(), (e) -> {
                return e.hasRefer();
            }), ReferCause::message, String.class)).messageLevel4Confirm();
        } else if (canDeleteIds.size() == ids.size()) {
            // 如果全部可以删除
            Result result = projectUserService.deleteByIdsLogical(canDeleteIds);
            return result;
        } else if (canDeleteIds.size() > 0 && canDeleteIds.size() < ids.size()) {
            // 如果部分行可以删除
            Result result = projectUserService.deleteByIdsLogical(canDeleteIds);
            if (result.failure()) {
                return result;
            } else {
                return ErrorDesc.success().message("已删除 " + canDeleteIds.size() + " 行，但另有 " + (ids.size() - canDeleteIds.size()) + " 行数据无法删除").data(canDeleteIds.size()).addErrors(CollectorUtil.collectArray(CollectorUtil.filter(causeMap.values(), (e) -> {
                    return e.hasRefer();
                }), ReferCause::message, String.class)).messageLevel4Confirm();
            }
        } else {
            // 理论上，这个分支不存在
            return ErrorDesc.success().message("数据删除未处理");
        }
    }

    /**
     * 更新项目人员
     */
    @ApiOperation(value = "更新项目人员")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectUserVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.IN_TIME, value = "加入时间", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.OUT_TIME, value = "离开时间", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.PROJECT_ID, value = "项目名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.STATUS, value = "人员状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.USER_ID, value = "项目人员", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.OWN_TYPE, value = "归属类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.ROLE, value = "角色", required = false, dataTypeClass = String.class)
	})
    @ApiParamSupport(ignoreDBTreatyProperties = true, ignoreDefaultVoProperties = true)
    @ApiOperationSupport(order = 4, author = "金杰 , maillank@qq.com", ignoreParameters = { ProjectUserVOMeta.PAGE_INDEX, ProjectUserVOMeta.PAGE_SIZE, ProjectUserVOMeta.SEARCH_FIELD, ProjectUserVOMeta.FUZZY_FIELD, ProjectUserVOMeta.SEARCH_VALUE, ProjectUserVOMeta.DIRTY_FIELDS, ProjectUserVOMeta.SORT_FIELD, ProjectUserVOMeta.SORT_TYPE, ProjectUserVOMeta.DATA_ORIGIN, ProjectUserVOMeta.QUERY_LOGIC, ProjectUserVOMeta.REQUEST_ACTION, ProjectUserVOMeta.IDS })
    @SentinelResource(value = ProjectUserServiceProxy.UPDATE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectUserServiceProxy.UPDATE)
    public Result update(ProjectUserVO projectUserVO) {
        Result result = projectUserService.update(projectUserVO, SaveMode.DIRTY_OR_NOT_NULL_FIELDS, false);
        return result;
    }

    /**
     * 保存项目人员
     */
    @ApiOperation(value = "保存项目人员")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectUserVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.IN_TIME, value = "加入时间", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.OUT_TIME, value = "离开时间", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.PROJECT_ID, value = "项目名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.STATUS, value = "人员状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.USER_ID, value = "项目人员", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.OWN_TYPE, value = "归属类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.ROLE, value = "角色", required = false, dataTypeClass = String.class)
	})
    @ApiParamSupport(ignoreDBTreatyProperties = true, ignoreDefaultVoProperties = true)
    @ApiOperationSupport(order = 5, ignoreParameters = { ProjectUserVOMeta.PAGE_INDEX, ProjectUserVOMeta.PAGE_SIZE, ProjectUserVOMeta.SEARCH_FIELD, ProjectUserVOMeta.FUZZY_FIELD, ProjectUserVOMeta.SEARCH_VALUE, ProjectUserVOMeta.DIRTY_FIELDS, ProjectUserVOMeta.SORT_FIELD, ProjectUserVOMeta.SORT_TYPE, ProjectUserVOMeta.DATA_ORIGIN, ProjectUserVOMeta.QUERY_LOGIC, ProjectUserVOMeta.REQUEST_ACTION, ProjectUserVOMeta.IDS })
    @SentinelResource(value = ProjectUserServiceProxy.SAVE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectUserServiceProxy.SAVE)
    public Result save(ProjectUserVO projectUserVO) {
        Result result = projectUserService.save(projectUserVO, SaveMode.DIRTY_OR_NOT_NULL_FIELDS, false);
        return result;
    }

    /**
     * 获取项目人员
     */
    @ApiOperation(value = "获取项目人员")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectUserVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class, example = "1")
	})
    @ApiOperationSupport(order = 6, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = ProjectUserServiceProxy.GET_BY_ID, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectUserServiceProxy.GET_BY_ID)
    public Result<ProjectUser> getById(String id) {
        Result<ProjectUser> result = new Result<>();
        ProjectUser projectUser = projectUserService.getById(id);
        // join 关联的对象
        projectUserService.dao().fill(projectUser).with("user").execute();
        projectUserService.dao().join(projectUser.getUser(), Person.class);

        String projectId=projectUser.getProjectId();
        //状态只统计，acting，wait，finish
        String sql1="select a.owner_id , \n" +
                "count(1) work_count,\n" +
                "sum(a.work_time) work_time\n" +
                "from oa_project_task_rcd a,oa_project_task b ,oa_project_user c\n" +
                "where a.task_id=b.id \n" +
                "and a.deleted=0 \n" +
                "and b.deleted=0\n" +
                "and b.task_status in ('wait','acting','finish')\n"+
                "and c.own_type='project'\n" +
                "and c.user_id=a.owner_id\n" +
                "and c.project_id=?\n" +
                "and c.project_id=a.project_id\n" +
                "group by a.owner_id";
        RcdSet rs1=projectUserService.dao().query(sql1,projectId);
        Map<Object, Rcd> map=rs1.getMappedRcds("owner_id");

        //统计任务
        String sql2="select \n" +
                "user_id,\n" +
                "sum(case task_status when 'acting' then cnt else 0 end) acting_cnt,\n" +
                "sum(case task_status when 'wait' then cnt else 0 end)   wait_cnt,\n" +
                "sum(case task_status when 'finish' then cnt else 0 end) finish_cnt,\n" +
                "sum(case task_status when 'close' then cnt else 0 end) close_cnt,\n" +
                "sum(case task_status when 'cancel' then cnt else 0 end) cancel_cnt\n" +
                " from (\n" +
                "select b.user_id,a.task_status ,count(1) cnt\n" +
                "from oa_project_task a,oa_project_user b \n" +
                "where a.id=b.project_id and a.deleted=0 and b.deleted=0  \n" +
                "and b.own_type='task'\n" +
                "and a.project_id=?\n"+
                "group by b.user_id,a.task_status \n" +
                ")tab \n" +
                "where user_id is not null and user_id<>''\n" +
                "group by user_id";
        RcdSet rs2=projectUserService.dao().query(sql2,projectId);
        Map<Object, Rcd> map2=rs2.getMappedRcds("user_id");
        String userId = projectUser.getUserId();
        // 工时统计
        if (map.containsKey(userId)) {
            Rcd rcd = map.get(userId);
            projectUser.setWorkTimeTotal(rcd.getString("work_time"));
            projectUser.setTaskRcdCount(rcd.getString("work_count"));
        }else{
            projectUser.setWorkTimeTotal("0");
            projectUser.setTaskRcdCount("0");
        }
        // 任务统计
        if (map2.containsKey(userId)) {
            Rcd rcd = map2.get(userId);
            int total = rcd.getInteger("acting_cnt") + rcd.getInteger("wait_cnt") + rcd.getInteger("finish_cnt");
            int acting = rcd.getInteger("acting_cnt") + rcd.getInteger("wait_cnt");
            projectUser.setTaskCompleteCount(rcd.getInteger("finish_cnt") + "");
            projectUser.setTaskCount(total + "");
            projectUser.setTaskActingCount(acting + "");
            if (total == 0) {
                projectUser.setTaskCompletePct("0");
            } else {
                projectUser.setTaskCompletePct((rcd.getInteger("finish_cnt") / total) + "");
            }
        } else {
            projectUser.setTaskCompleteCount("0");
            projectUser.setTaskCount("0");
            projectUser.setTaskActingCount("0");
            projectUser.setTaskCompletePct("0");
        }

        result.success(true).data(projectUser);
        return result;
    }

    /**
     * 批量获取项目人员 <br>
     * 联合主键时，请自行调整实现
     */
    @ApiOperation(value = "批量获取项目人员")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectUserVOMeta.IDS, value = "主键清单", required = true, dataTypeClass = List.class, example = "[1,3,4]")
	})
    @ApiOperationSupport(order = 3, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = ProjectUserServiceProxy.GET_BY_IDS, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectUserServiceProxy.GET_BY_IDS)
    public Result<List<ProjectUser>> getByIds(List<String> ids) {
        Result<List<ProjectUser>> result = new Result<>();
        List<ProjectUser> list = projectUserService.queryListByIds(ids);
        result.success(true).data(list);
        return result;
    }

    /**
     * 查询项目人员
     */
    @ApiOperation(value = "查询项目人员")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectUserVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.IN_TIME, value = "加入时间", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.OUT_TIME, value = "离开时间", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.PROJECT_ID, value = "项目名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.STATUS, value = "人员状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.USER_ID, value = "项目人员", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.OWN_TYPE, value = "归属类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.ROLE, value = "角色", required = false, dataTypeClass = String.class)
	})
    @ApiOperationSupport(order = 5, author = "金杰 , maillank@qq.com", ignoreParameters = { ProjectUserVOMeta.PAGE_INDEX, ProjectUserVOMeta.PAGE_SIZE })
    @SentinelResource(value = ProjectUserServiceProxy.QUERY_LIST, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectUserServiceProxy.QUERY_LIST)
    public Result<List<ProjectUser>> queryList(ProjectUserVO sample) {
        Result<List<ProjectUser>> result = new Result<>();
        List<ProjectUser> list = projectUserService.queryList(sample);
        result.success(true).data(list);
        return result;
    }

    /**
     * 分页查询项目人员
     */
    @ApiOperation(value = "分页查询项目人员")
    @ApiImplicitParams({
		@ApiImplicitParam(name = ProjectUserVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.IN_TIME, value = "加入时间", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.OUT_TIME, value = "离开时间", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.PROJECT_ID, value = "项目名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.STATUS, value = "人员状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.USER_ID, value = "项目人员", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.OWN_TYPE, value = "归属类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = ProjectUserVOMeta.ROLE, value = "角色", required = false, dataTypeClass = String.class)
	})
    @ApiOperationSupport(order = 8, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = ProjectUserServiceProxy.QUERY_PAGED_LIST, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(ProjectUserServiceProxy.QUERY_PAGED_LIST)
    public Result<PagedList<ProjectUser>> queryPagedList(ProjectUserVO sample) {
        Result<PagedList<ProjectUser>> result = new Result<>();
        PagedList<ProjectUser> list = projectUserService.queryPagedList(sample, sample.getPageSize(), sample.getPageIndex());
        // join 关联的对象
        String projectId=sample.getProjectId();
        projectUserService.dao().fill(list).with("user").execute();
        List<Employee> users = CollectorUtil.collectList(list, ProjectUser::getUser);
        projectUserService.dao().join(users, Person.class);

        if(!StringUtil.isBlank(projectId)){
            //状态只统计，acting，wait，finish
            String sql1="select a.owner_id , \n" +
                    "count(1) work_count,\n" +
                    "sum(a.work_time) work_time\n" +
                    "from oa_project_task_rcd a,oa_project_task b ,oa_project_user c\n" +
                    "where a.task_id=b.id \n" +
                    "and a.deleted=0 \n" +
                    "and b.deleted=0\n" +
                    "and b.task_status in ('wait','acting','finish')\n"+
                    "and c.own_type='project'\n" +
                    "and c.user_id=a.owner_id\n" +
                    "and c.project_id=?\n" +
                    "and c.project_id=a.project_id\n" +
                    "group by a.owner_id";
            RcdSet rs1=projectUserService.dao().query(sql1,projectId);
            Map<Object, Rcd> map=rs1.getMappedRcds("owner_id");

            //统计任务
            String sql2="select \n" +
                    "user_id,\n" +
                    "sum(case task_status when 'acting' then cnt else 0 end) acting_cnt,\n" +
                    "sum(case task_status when 'wait' then cnt else 0 end)   wait_cnt,\n" +
                    "sum(case task_status when 'finish' then cnt else 0 end) finish_cnt,\n" +
                    "sum(case task_status when 'close' then cnt else 0 end) close_cnt,\n" +
                    "sum(case task_status when 'cancel' then cnt else 0 end) cancel_cnt\n" +
                    " from (\n" +
                    "select b.user_id,a.task_status ,count(1) cnt\n" +
                    "from oa_project_task a,oa_project_user b \n" +
                    "where a.id=b.project_id and a.deleted=0 and b.deleted=0  \n" +
                    "and b.own_type='task'\n" +
                    "and a.project_id=?\n"+
                    "group by b.user_id,a.task_status \n" +
                    ")tab \n" +
                    "where user_id is not null and user_id<>''\n" +
                    "group by user_id";
            RcdSet rs2=projectUserService.dao().query(sql2,projectId);
            Map<Object, Rcd> map2=rs2.getMappedRcds("user_id");
            for (int i = 0; i < list.getList().size(); i++) {
                String userId = list.getList().get(i).getUserId();
                // 工时统计
                if (map.containsKey(userId)) {
                    Rcd rcd = map.get(userId);
                    list.getList().get(i).setWorkTimeTotal(rcd.getString("work_time"));
                    list.getList().get(i).setTaskRcdCount(rcd.getString("work_count"));
                }else{
                    list.getList().get(i).setWorkTimeTotal("0");
                    list.getList().get(i).setTaskRcdCount("0");
                }


                // 任务统计
                if (map2.containsKey(userId)) {
                    Rcd rcd = map2.get(userId);
                    int total = rcd.getInteger("acting_cnt") + rcd.getInteger("wait_cnt") + rcd.getInteger("finish_cnt");
                    int acting = rcd.getInteger("acting_cnt") + rcd.getInteger("wait_cnt");
                    list.getList().get(i).setTaskCompleteCount(rcd.getInteger("finish_cnt") + "");
                    list.getList().get(i).setTaskCount(total + "");
                    list.getList().get(i).setTaskActingCount(acting + "");
                    if (total == 0) {
                        list.getList().get(i).setTaskCompletePct("0");
                    } else {
                        list.getList().get(i).setTaskCompletePct(   ((rcd.getInteger("finish_cnt") / total))*100    + "");
                    }
                } else {
                    list.getList().get(i).setTaskCompleteCount("0");
                    list.getList().get(i).setTaskCount("0");
                    list.getList().get(i).setTaskActingCount("0");
                    list.getList().get(i).setTaskCompletePct("0");
                }
            }
        }
        result.success(true).data(list);
        return result;
    }
}
